﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.Drawing;
namespace ReconCaracteres_Backpropagation
{
    public class Sample
    {
        public StreamReader reader;
        public String ID;
        public String category;
       
        public int categoryIndex;
        public Double[] expectedVector;
        public Double[,] matrix = new Double[10, 10];

        public Sample(Double[,] matrix, int categoryIndex) {
            this.matrix = matrix;
            this.categoryIndex = categoryIndex;
        }

        public Sample(String fileName) {
            this.matrix = this.readTXTSample(fileName);
            this.ID = System.IO.Path.GetFileName(fileName);
        }

        public void setExpectedVector( int categoryAmount){
            this.expectedVector = this.getExpectedVector(categoryIndex, categoryAmount);
        }

        /// <summary>
        /// Read a sample (matrix) from a txt file
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns></returns>
        public Double[,] readTXTSample(String fileName)
        {
            reader = new StreamReader(fileName);


            String line = "";
            String[] values = null;
            Double[,] matrix = new Double[10, 10];
            int rowCounter = 0;
            while ((line = reader.ReadLine()) != null && rowCounter<10)
            {
                values = line.Split(' ');
                for (int i = 0; i < values.Count(); i++)
                {
                    matrix[i,rowCounter ] = Double.Parse(values[i]);
     
                }
                rowCounter++;
            }
            reader.Close();
            this.matrix = matrix;


            
            return matrix;
        }
        public Bitmap getBitmap()
        {
            Bitmap bitmap = new Bitmap(matrix.GetLength(0), matrix.GetLength(1));
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    if (1 == matrix[i, j])
                    {
                        bitmap.SetPixel(i, j, Color.Black);
                    }
                    else {
                        bitmap.SetPixel(i, j, Color.White);
                    }
                }
            }
            return bitmap;
        }
        public override string ToString()
        {
            //return this.ID+" ("+this.category+")";
            return "Categoría "+this.categoryIndex;
        }
        public Double[] getInputVector(){
            Double [] inputVector= new Double[matrix.GetLength(0)*matrix.GetLength(1)];
            int counter=0;
            for (int i = 0; i < matrix.GetLength(0); i++){
                for (int j = 0; j < matrix.GetLength(1); j++)
                {
                    inputVector[counter] = matrix[i, j];
                    counter++;
                }
            }
            return inputVector;
        }
        /// <summary>
        /// Gets the expected vector.
        /// </summary>
        /// <param name="categoryIndex">Index of the category.</param>
        /// <param name="categoryAmount">The category amount.</param>
        /// <returns></returns>
        public Double[] getExpectedVector(int categoryIndex, int categoryAmount){
            int bitsRequired = Convert.ToInt32(Math.Ceiling(Math.Log(categoryAmount, 2)));//bits required to store the decimal numbers in binary format
            Double[] expectedVector = new Double[bitsRequired];
            String bits = Convert.ToString(categoryIndex, 2);
            for (int i = 0; i < expectedVector.Count(); i++ )
            {
                if (i<bits.Length)
                {
                    expectedVector[i] = Double.Parse(bits.Substring(bits.Length-i-1, 1));
                }
                else {
                    expectedVector[i] = 0.0;
                }
            }
            return expectedVector;
        }
        /// <summary>
        /// Adapter
        /// </summary>
        /// <param name="expectedVector">The expected vector.</param>
        /// <returns></returns>
        public Double[] getVectorD(Double[] expectedVector)
        {
            Double[] d = new Double[expectedVector.Length+1];
            d[0] = 0;
            for (int i = 1; i < d.Length; i++)
            {
                d[i] = expectedVector[i - 1];
            }
            return d;
        }
    }
}
